﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using IO = System.IO;
using GameListCreator.ViewModels;
using System.Windows.Media.Animation; 

namespace GameListCreator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = App.GameListViewModel;

            this.HideControls();

            App.GameListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GameListViewModel_PropertyChanged);
        }

        /// <summary>
        /// Cache les Controls Selector et Editor
        /// </summary>

        private void HideControls()
        {
            this.GameSelector.Opacity = 0;
            this.GameSelector.Visibility = System.Windows.Visibility.Visible;

            this.GameEditor.Opacity = 0;
            this.GameEditor.Visibility = System.Windows.Visibility.Visible;

            this.StackPanelMessage.Visibility = System.Windows.Visibility.Visible;
            this.GridSplitter.Visibility = System.Windows.Visibility.Collapsed;        
        }

        /// <summary>
        /// Changement de la propriété
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void GameListViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case GameListViewModel.GameListFullFilenamePropertyName :

                    if (App.GameListViewModel.GameListFullFilename != null)
                    {
                        this.Title = "GameListCreator - " + App.GameListViewModel.GameListFullFilename;
                    }
                    else
                    {
                        this.Title = "GameListCreator";
                    
                        // on revient à la normal

                        this.GridSplitter.Visibility = System.Windows.Visibility.Collapsed;
                        
                        Storyboard storyboardClose = this.Resources["StoryboardClose"] as Storyboard;

                        storyboardClose.BeginTime = TimeSpan.FromMilliseconds(500);
                        storyboardClose.Begin();
                    }

                    break;

                case GameListViewModel.IsLoadedWithNoErrorPropertyName :

                    if (App.GameListViewModel.IsLoadedWithNoError == true)
                    {
                        this.GridSplitter.Visibility = System.Windows.Visibility.Visible;

                        Storyboard storyboardOpen = this.Resources["StoryboardOpen"] as Storyboard;
                        
                        storyboardOpen.BeginTime = TimeSpan.FromMilliseconds(500);
                        storyboardOpen.Begin();
                    }

                    break;
            }
        }

        /// <summary>
        /// Ouverture
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonOpen_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openDialog = new OpenFileDialog();

                openDialog.DefaultExt = "*.xml|Fichier XML";
                openDialog.FileName = "GameList.xml";

                if (openDialog.ShowDialog(this) == true)
                {
                    string filename = openDialog.FileName;

                    App.GameListViewModel.Load(filename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error during the opening of the GameList file : " + ex.Message);
            }
        }

        /// <summary>
        /// Création
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonNew_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveFileDialog saveDialog = new SaveFileDialog();

                saveDialog.DefaultExt = "*.xml|Fichier XML";
                saveDialog.FileName = "GameList.xml";

                if (saveDialog.ShowDialog(this) == true)
                {
                    string filename = saveDialog.FileName;

                    App.GameListViewModel.New(filename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error during the creation of the GameList file : " + ex.Message);
            }
        }

        /// <summary>
        /// Sauver
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                App.GameListViewModel.Save();

                MessageBox.Show("GameList saved !", "", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error during the saving of the GameList file : " + ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        /// <summary>
        /// Nouveau jeux
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonNewGame_Click(object sender, RoutedEventArgs e)
        {
            App.GameListViewModel.AddGame("GameBoy");
        }

        /// <summary>
        /// Destruction de tous les jeux
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Dou you want to remove this GameList (Roms and Covers directories included) ?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                App.GameListViewModel.Remove();
            }
        }
    }
}
